﻿<%@ WebHandler Language="C#" Class="instructionsmatching" %>

using System;
using System.Web;
using System.Text;
using System.Data;

public class instructionsmatching : IHttpHandler {

    public void ProcessRequest(HttpContext context){
        context.Response.ContentType = "application/json";
        string id = context.Request.Params["id"];
        string drug_name = context.Request.Params["drug_name"];
        string drug_changjia = context.Request.Params["drug_changjia"];
        string batch_number = context.Request.Params["batch_number"];

        if (string.IsNullOrEmpty(drug_name) && string.IsNullOrEmpty(drug_changjia))
        {
            context.Response.Write("{\"code\":0,\"msg\":\"\u5339\u914d\u5931\u8d25\uff0c\u836f\u54c1\u540d\u79f0\u548c\u6279\u6b21\u53f7\u4e0d\u80fd\u4e3a\u7a7a\"}");
        }
        else
        {
            context.Response.Write(Data(id, drug_name, drug_changjia));
        }
    }

    MsSqlHelper mssql = new MsSqlHelper();

    public string Data(string id, string k, string changjia)
    {
        string sql = "select top 48 a.spid,rtrim(a.spbh) as spbh,rtrim(a.spmch) as spmch,rtrim(a.shpgg) as shpgg,rtrim(a.dw) as dw,rtrim(a.shengccj) as shengccj,rtrim(b.detailid) as detailid ";
        sql +=" from spkfk a(nolock) left join [ImageDoc] b(nolock) on a.spid=b.detailid and b.type_id='pj' ";
        sql +=" where (spbh like '%" + k + "%' or spmch like '%" + k + "%' or zjm like '%" + k + "%' ) and shengccj like '%" + changjia + "%' ";
        sql +=" group by a.spid,a.spbh,a.spmch,a.shpgg,a.dw,a.shengccj,b.detailid ";
        DataSet ds = mssql.ExecuteDs(sql);

        string retsult = "";
        retsult = "{\"code\":1,\"html\":\"<table width=\\\"100%\\\" border=\\\"0\\\" cellspacing=\\\"0\\\" cellpadding=\\\"0\\\"  class=\\\"sebig\\\"><tr class=\\\"setit\\\"><td>药品名称</td><td>药品编号</td><td>规格</td><td >单位</td><td>生产厂家</td><td>批件</td><td>操作</td></tr>";
        foreach (DataRow row in ds.Tables[0].Rows)
        {

            retsult = retsult + "<tr class=\\\"setit\\\"><td>" + row["spmch"] + "</td><td>" + row["spbh"] + "</td><td>" + row["shpgg"] + "</td><td>" + row["dw"] + "</td><td>" + row["shengccj"] + "</td>";

            if (string.IsNullOrEmpty(row["detailid"].ToString()))
            {
                retsult = retsult + "<td>未上传</td>";
                retsult = retsult + "<td class=\\\"QiyeC\\\"><a class=\\\"save\\\" data-img=\\\"" + id + "\\\" data-id=\\\"" + row["spid"] + "\\\" data-way=\\\"cover\\\" href=\\\"javascript:;\\\">保存</a></td>";
            }
            else
            {
                retsult = retsult + "<td class=\\\"QiyeC\\\">";            
                sql = "select imageid,url from [ImageDoc](nolock) where detailid='"+row["detailid"].ToString()+"' order by imageid";
                DataSet dss = mssql.ExecuteDs(sql);

                for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        retsult = retsult + "<a class=\\\"views\\\" href=\\\"" + dss.Tables[0].Rows[i]["url"].ToString() + "\\\" data-img=\\\"" + dss.Tables[0].Rows[i]["url"].ToString() + "\\\" rel=\\\"" + row["detailid"].ToString() + "\\\" class=\\\"view\\\">预览</a>";  
                    }
                    else
                    {
                        retsult = retsult + "<a class=\\\"views\\\" href=\\\"" + dss.Tables[0].Rows[i]["url"].ToString() + "\\\" data-img=\\\"" + dss.Tables[0].Rows[i]["url"].ToString() + "\\\" rel=\\\"" + row["detailid"].ToString() + "\\\" class=\\\"view\\\" style=\\\"display:none;\\\">预览</a>";  
                    }
                }
                retsult = retsult + "</td>";
                
                retsult = retsult + "<td class=\\\"QiyeC\\\">"
    + "<a style=\\\"margin-right:20px;\\\" class=\\\"save\\\" data-img=\\\"" + id + "\\\" data-id=\\\"" + row["spid"] + "\\\" data-way=\\\"cover\\\" href=\\\"javascript:;\\\">覆盖保存</a>"
    + "<a class=\\\"save\\\" data-img=\\\"" + id + "\\\" data-way=\\\"add\\\" data-id=\\\"" + row["spid"] + "\\\" href=\\\"javascript:;\\\">新增保存</a></td>";
            }
            retsult = retsult + "</tr>";
        }
        retsult = retsult + "</table>\"}";
        return retsult;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    /// <summary>
    /// <summary>
    /// 字符串转Unicode
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <returns>Unicode编码后的字符串</returns>
    public static string String2Unicode(string source)
    {
        byte[] bytes = Encoding.Unicode.GetBytes(source);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < bytes.Length; i += 2)
        {
            stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
        }
        return stringBuilder.ToString();
    }

}